package com.artisan.provider.service.impl;

import com.artisan.provider.domain.ZgUserJurisdiction;
import com.artisan.provider.domain.ZgUserMenu;
import com.artisan.provider.dto.StringListDto;
import com.artisan.provider.dto.ZgUserParamDto;
import com.artisan.provider.po.ZgUserMenuPo;
import com.artisan.provider.mapper.ZgUserJurisdictionMapper;
import com.artisan.provider.mapper.ZgUserMenuMapper;
import com.artisan.provider.service.ZgUserJurisdictionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@Service
public class ZgUserJurisdictionServiceImpl implements ZgUserJurisdictionService {

    @Autowired
    ZgUserJurisdictionMapper zgUserJurisdictionMapper;

    @Autowired
    ZgUserMenuMapper zgUserMenuMapper;

    @Override
    public List<ZgUserJurisdiction> jurisdictionList() {
        return zgUserJurisdictionMapper.selectAll();
    }

    @Override
    public List<ZgUserMenu> juidictionMenu(ZgUserParamDto zgUserParamDto) {
        Integer[] integers = juridictionMenu(zgUserParamDto.getMenuList());

        List<ZgUserMenu> zgUserMenus0 = new ArrayList<>();
        for (Integer s : integers) {

            System.out.println(s);
            ZgUserMenu zgUserMenu5 = new ZgUserMenu();
            ZgUserMenu zgUserMenu6 = zgUserMenuMapper.selectOneList(s);
            zgUserMenu5 = zgUserMenu6;

            List<ZgUserMenu> zgUserMenu = zgUserMenuMapper.selectTwoList(zgUserMenu5.getmId()); //select parentId

            if (zgUserMenu.size() > 0){
                List<ZgUserMenu> menuList1 = new ArrayList<>();
                for (ZgUserMenu userMenu : zgUserMenu) {
                    ZgUserMenu userMenu1 = userMenu;
                    if (userMenu1.getRoutes() == null){
                        userMenu1.setRoutes(new ArrayList<>());
                          if (zgUserParamDto.getMenuList().contains(userMenu1.getmId().toString())){
                              menuList1.add(userMenu1);
                          }
                    }
                 }
                zgUserMenu5.setRoutes(menuList1);
                zgUserMenus0.add(zgUserMenu5);
            }else{
                if(zgUserMenu5.getParentId() == 0){
                    zgUserMenu5.setRoutes(new ArrayList<>());
                    zgUserMenus0.add(zgUserMenu5);
                }
            }
        }

        return zgUserMenus0;
    }

    @Override
    public List<ZgUserMenuPo> jurisdictionMenuList() {
        return zgUserMenuMapper.userMenuJurisdictionList();
    }

    @Override
    public int addJurisdictionMenu(ZgUserJurisdiction zgUserJurisdiction) {
        zgUserJurisdictionMapper.insertSelective(zgUserJurisdiction);
        return 0;
    }

    @Override
    public int updateJurisdictionMenu(ZgUserJurisdiction zgUserJurisdiction) {
        zgUserJurisdictionMapper.updateByPrimaryKeySelective(zgUserJurisdiction);
        return 0;
    }

    @Override
    public List<String> juidictionMenuPath(ZgUserParamDto zgUserParamDto) {
        Integer[] integers = juridictionMenu(zgUserParamDto.getMenuList());
        List<Integer> integerPath = new ArrayList<>();
        for (Integer integer : integers) {
            integerPath.add(integer);
        }
        StringListDto stringListDto = new StringListDto();
        stringListDto.setList(integerPath);
        List<String> stringList = zgUserMenuMapper.selectFindPathList(stringListDto);
        return stringList;
    }

    public Integer[] juridictionMenu(List<String> strs){

        List<Integer> integers = new ArrayList<>();
        // p_id   parent_id
        List<String> menuList = strs;
        for (String s : menuList) {
            integers.add(Integer.valueOf(s));
        }

        Integer[] arr = integers.toArray(new Integer[]{});
        List<Integer> stringList = Arrays.asList(arr);

        StringListDto stringListDto = new StringListDto();
        stringListDto.setList(stringList);
        //zi get fu
        List<Integer> zgUserZi = new ArrayList<>();
        List<Integer>  zgUserMenus = zgUserMenuMapper.selectMenuList(stringListDto);
        //zgUserZi = zgUserMenus;
        StringListDto stringListDto1 = new StringListDto();
        List<Integer> strList = new ArrayList<>();
        strList = stringList;
        HashSet<Integer> integers1 = new HashSet<>(zgUserMenus);
        Integer[] array = new Integer[integers1.size()];
        integers1.toArray(array);

        for (Integer integer : array) {
            zgUserZi.add(integer);
        }
        for (Integer integer : stringList) {
            zgUserZi.add(integer);
        }

        HashSet<Integer> hashSet = new HashSet<>(zgUserZi);
        List<Integer> listArray = new ArrayList<>(hashSet);
        Integer[] arrStr = listArray.toArray(new Integer[]{});
        //冒泡排序算法
        for (int i = 0; i < arrStr.length; i++) {
            for (int j = 0; j < arrStr.length; j++) {
                if(arrStr[i] < arrStr[j]){
                    int tmp = arrStr[i];
                    arrStr[i] = arrStr[j];
                    arrStr[j] = tmp;
                }
            }
        }

        return arrStr;
    }
}
